<?php

	Class Yomm_Library_CmsPageParserFunctions {
		
		protected $args = array();
		
		protected $call = null;
		
		protected $output = null;
		
		protected $view = array();
		
		protected $theme = null;
		
		public function __construct($call, $args, $view, $theme)
		{
			$call = "function_{$call}";
			
			if(!method_exists($this, $call))
			{
				$this->set_output("[error: the function '$call' does not exists]"); return null;
			}
			
			$this->args = $args; $this->call = $call; $this->view = $view; $this->theme = $theme;
			
			$this->$call();
		}
		
		public function set_output($text)
		{
			$this->output = $text;
		}
		
		public function get_output()
		{
			return $this->output;
		}
		
		private function ext_decode($text)
		{
			return nl2br(htmlentities(utf8_decode($text)), ENT_QUOTES);
		}
		
		private function inspect_args($must_have_args)
		{
			foreach($must_have_args as $inspect)
			{
				if(!array_key_exists($inspect, $this->args))
				{
					$this->set_output("[error: method '{$this->call}' requires attribute '{$inspect}']");
					
					return false;
				}
			}
			
			return true;
		}
		
		/*********************
		 * FUNCTIONS
		 *********************/
		
		/**
		 * copy the RENDERED CONTENT of a page and return it
		 * 
		 * @param name [string] the rewrite name of the page
		 */
		public function function_copy()
		{
			if(!$this->inspect_args(array('name'))) return false;

			$_obj = new ParseObject($this->args['name'], $this->theme, $this->view);
			
			$return = $_obj->export();
			
			$this->set_output($return->text);
		}
		
		/**
		 * include a template file for the current activ theme and return it.
		 * 
		 * @param tpl [string] the template name in the current theme
		 */
		public function function_include()
		{
			if(!$this->inspect_args(array('tpl'))) return false;
			
			$file = "theme/{$this->theme}/tpl/{$this->args['tpl']}";
			
			if(!file_exists($file))
			{
				$this->set_output("[include file '{$file}' does not exists]"); return false;
			}
			
			$this->set_output($this->view->fetch($file));
		}
		
		/**
		 * load and execute a controller!
		 */
		public function function_controller()
		{
			if(!$this->inspect_args(array("name","method"))) return false;
			
			$yomm_boot = new Yomm_Boot();
	
			$yomm_boot_return = $yomm_boot->doReturn($this->args['name'], $this->args['method']);
			
			$this->view->assign($yomm_boot->getView()->getTemplateVars());
			
			$this->set_output($yomm_boot_return);
		}
		
		/**
		 * @param account [char] the twitter account u want to access
		 * @param tpl [char] the template which will content the output buffer in var $tweets
		 * 
		 * @param { max [char] optional maximal tweets param ; default is 25 }
		 * 
		 * @return $tweets [array]
		 */
		public function function_twitter()
		{
			if(!$this->inspect_args(array('account', 'tpl'))) return false;
			
			$tw = new Twitter($this->args['account']);
			
			if(isset($this->args['max']))
			{
				$tw->setEndTweet($this->args['max']);
			}
			
			$this->view->assign('twitter', $tw->getTweets());
			
			$this->set_output($this->view->fetch($this->args['tpl']));
		}
		
		/**
		 * assign the values of an rss file into the template 'tpl' and return it
		 * 
		 * @param url
		 * @param tpl
		 * 
		 * example url: http://www.facebook.com/feeds/notes.php?id=128493650677&viewer=1486032291&key=1f25b92f60&format=rss20
		 */
		public function function_rss()
		{
			if(!$this->inspect_args(array('url', 'tpl'))) return false;
			
			$doc = new DOMDocument();
			
			//$doc->load('test.xml');
			//$doc->load('http://www.facebook.com/feeds/notes.php?id=128493650677&viewer=1486032291&key=1f25b92f60&format=rss20');
			
			$header = array('http' =>
			    array(
			        'method'  => 'GET',
			        'header'  => 'User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.38 Safari/533.4\r\n'.
			    	'Referer: http://www.facebook.com/\r\n'.
			    	'Accept:application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
			    )
			);
			
			$header_ctx = stream_context_create($header);
			
			$txt = file_get_contents($this->args['url'], false, $header_ctx);
			
			$doc->loadXML($txt);
			
			$rss = array();
			
			foreach($doc->getElementsByTagName('item') as $node)
			{
				$rss[] = array( 
					'title' => $this->ext_decode($node->getElementsByTagName('title')->item(0)->nodeValue),
					'desc' => $this->ext_decode($node->getElementsByTagName('description')->item(0)->nodeValue),
					'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
					'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
				);
			}
			
			$this->view->assign('rss', $rss);
			
			if(!file_exists($this->view->template_dir . $this->args['tpl']))
			{
				$this->set_output("[error: template file '{$this->view->template_dir}{$this->args['tpl']}' does not exists!]"); return false;
			}
			
			$this->set_output($this->view->fetch($this->args['tpl']));
		}
	}